package com.lixixi.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.lixixi.tingshu.album.service.BaseCategoryService;
import com.lixixi.tingshu.album.service.BaseCategoryViewService;
import com.lixixi.tingshu.common.result.Result;
import com.lixixi.tingshu.model.album.BaseAttribute;
import com.lixixi.tingshu.model.album.BaseCategory3;
import com.lixixi.tingshu.model.album.BaseCategoryView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/album/category")
public class BaseCategoryApiController {
	@Autowired
	BaseCategoryViewService baseCategoryViewService;

	@Autowired
	BaseCategoryService baseCategoryService;

	@GetMapping("/getBaseCategoryList")
	Result<List<JSONObject>> getBaseCategoryList(){
		List<JSONObject> jsonObjectList = baseCategoryViewService.findBaseCategory();
		return Result.ok(jsonObjectList);
	}

	@GetMapping("/getBaseCategoryList/{category1Id}")
	Result<JSONObject> getBaseCategoryList(@PathVariable("category1Id") Long category1Id){
		List<JSONObject> jsonObject2List = baseCategoryViewService.findBaseCategory(category1Id);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("categoryChild", jsonObject2List);
		return Result.ok(jsonObject);
	}

	@GetMapping("/findAttribute/{category1Id}")
	Result<List<BaseAttribute>> findAttribute(@PathVariable("category1Id")Long category1Id){
		return Result.ok(baseCategoryService.findBaseCategory1Attribute(category1Id));
	}

	@GetMapping("/findCategoryByCategory3Id/{category3Id}")
	Result<BaseCategoryView> findCategoryByCategory3Id(@PathVariable("category3Id")Long category3Id){
		return Result.ok(baseCategoryViewService.getById(category3Id));
	}

	@GetMapping("/findCategoryByCategory3IdBatch")
	Result<Map<Long, List<BaseCategoryView>>> findCategoryByCategory3IdBatch(@RequestBody List<Long> category3IdList){
		Map<Long, List<BaseCategoryView>> baseCategoryViewMap = baseCategoryViewService.listByIds(category3IdList).stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
		return Result.ok(baseCategoryViewMap);
	}

	@GetMapping("/findTopBaseCategory3/{categoryId1}")
	Result<List<BaseCategory3>> findTopBaseCategory3(@PathVariable("categoryId1")Long categoryId1){
		List<BaseCategory3> baseCategory3List = baseCategoryService.findTopBaseCategory3(categoryId1);
		return Result.ok(baseCategory3List);
	}

	@GetMapping("/findBaseCategory3/{categoryId1}")
	Result<List<BaseCategory3>> findBaseCategory3(@PathVariable("categoryId1")Long categoryId1){
		List<BaseCategory3> baseCategory3List = baseCategoryService.findBaseCategory3(categoryId1);
		return Result.ok(baseCategory3List);
	}

	@GetMapping("/getCategory1IdAll")
	Result<List<Long>> getCategory1IdAll(){
		return Result.ok(baseCategoryService.getCategory1IdAll());
	}

}
